package com.hzdl.apple.controller.homeCotroll;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hzdl.apple.bean.Good;
import com.hzdl.apple.bean.User;
import com.hzdl.apple.service.GoodsService;
import com.hzdl.apple.utils.HttpUtils;
@WebServlet("/home/*")
public class HomeController extends HttpServlet {
	private GoodsService goodsService;
	
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		goodsService=new GoodsService();
		getServletContext().setAttribute("path", getServletContext().getContextPath());

		
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		String baseUri = req.getRequestURI();
		String GoodsName=req.getParameter("GoodsName");
		if (baseUri.contains("/index")){
			req.getRequestDispatcher("/WEB-INF/jsp/home/index.jsp").forward(req, resp);
		}else if(baseUri.contains("/login")){
			req.getRequestDispatcher("/WEB-INF/jsp/home/user-login.jsp").forward(req, resp);
		}else if(baseUri.contains("/good")){
			
			List<Good> goods=goodsService.ShowImg(GoodsName);
			String[] a = goods.get(0).getGoodsImgForShow().split(" ");
			req.setAttribute("a", a);
			req.setAttribute("b",GoodsName);
			req.getRequestDispatcher("/WEB-INF/jsp/home/goods.jsp").forward(req, resp);
		}else if(baseUri.contains("/regist")){
		req.getRequestDispatcher("/WEB-INF/jsp/home/user-regist.jsp").forward(req, resp);
//			HttpUtils.forward("user-regist.jsp", req, resp);
		}else if(baseUri.contains("/psw")){
			req.getRequestDispatcher("/WEB-INF/jsp/home/user-psw.jsp").forward(req, resp);
		}else if(baseUri.contains("/quit")){
			req.getSession().invalidate();
			req.setAttribute("LoginOutMessage", "注销成功");
			req.getRequestDispatcher("/WEB-INF/jsp/home/index.jsp").forward(req, resp);
		}else if(baseUri.contains("/msg")){
			req.getRequestDispatcher("/WEB-INF/jsp/home/user-msg.jsp").forward(req, resp);
		}else if(baseUri.contains("/info")){
			req.getRequestDispatcher("/WEB-INF/jsp/home/user-info.jsp").forward(req, resp);
		}else if(baseUri.contains("/find")){
			req.getRequestDispatcher("/WEB-INF/jsp/home/user-find.jsp").forward(req, resp);
		}
	}
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	
		super.doPost(req, resp);
	}
}
